package cn.tedu.vrd.controller;

import cn.tedu.vrd.mapper.ProductMapper;
import cn.tedu.vrd.vo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class ProductController {

    @Autowired
    ProductMapper mapper;

    @RequestMapping("/product/insert")
    public void insert(Product product, MultipartFile picFile) throws IOException {
        String fileName = picFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;
        //文件夹  E:/upload/2022/01/20/xxx.jpg
        String dirPath = "D:/upload";
        //创建日期格式化对象
        SimpleDateFormat f =
                new SimpleDateFormat("/yyyy/MM/dd/");
        //把当前日期对象 转成上面指定的格式  datePath= /2022/01/19/
        String datePath = f.format(new Date());
        dirPath = dirPath+datePath;
        //创建文件对象
        File dirFile = new File(dirPath);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //完整文件路径
        String filePath = dirPath+fileName;
        picFile.transferTo(new File(filePath));

        product.setUrl(datePath+fileName);
        product.setCreated(new Date());
        mapper.insert(product);
    }

    @RequestMapping("/product/select/admin")
    public List<Product> selectAdmin(){
        //查询出供后台管理页面显示的作品数据
        return mapper.selectAdmin();
    }
    @RequestMapping("/product/delete")
    public void delete(int id){
        //得到文件的url
        String url = mapper.selectUrlById(id);
        //得到文件的完整路径
        String filePath = "E:/upload"+url;
        new File(filePath).delete();//删除文件
        //删除数据库中的数据
        mapper.delete(id);
    }
    @RequestMapping("/product/select")
    public List<Product> select(){
        return mapper.select();
    }

    @RequestMapping("/product/select/view")
    public List<Product> selectView(){
        return mapper.selectView();
    }

    @RequestMapping("/product/select/like")
    public List<Product> selectLike(){
        return mapper.selectLike();
    }

    @RequestMapping("/product/selectByCid")
    public List<Product> selectByCid(int cid){
        return mapper.selectByCid(cid);
    }

    @RequestMapping("/product/selectByWd")
    public List<Product> selectByWd(String wd){
        return mapper.selectByWd(wd);
    }

    @RequestMapping("/product/selectById")
    public Product selectById(int id, HttpSession session){
        String info = (String) session.getAttribute("view"+id);
        if (info == null){ //浏览过了
            mapper.updateViewCount(id);//浏览量+1
            session.setAttribute("view"+id,"isView"); //把浏览过的id保存到session中
        }
        return mapper.selectById(id);
    }

    @RequestMapping("/product/like")
    public int like(int id, HttpSession session){
        //从Session中获取信息  如果获取得到代表点过赞 反之没有点过赞
        String info = (String) session.getAttribute("like"+id);
        if (info != null){ //点过赞了
            return 2;
        }
        mapper.updateLikeCount(id); //点赞
        session.setAttribute("like"+id,"isLike"); //把点过赞的id保存到session中
        return 1; //点赞成功
    }

    @RequestMapping("/product/deleteByCid")
    public void deleteByCid(int cid){
        mapper.deleteByCid(cid);
    }
}
